package com.waves.message.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.waves.core.constant.DbConstants;
import com.waves.core.support.EntityOperations;
import com.waves.core.utils.LoginUtils;
import com.waves.core.utils.StringUtils;
import com.waves.core.web.domain.R;
import com.waves.core.web.domain.dto.MessageRecordInsertDTO;
import com.waves.message.domain.dto.MessageRecordPageDTO;
import com.waves.message.domain.entity.MessageRecord;
import com.waves.message.events.ISendMessageEvent;
import com.waves.message.mapper.MessageRecordMapper;
import com.waves.message.service.MessageRecordService;
import com.waves.message.vo.MessageRecordPageVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @author wzw
 * @description 针对表【message_record】的数据库操作Service实现
 * @createDate 2025-01-01 15:01:07
 */
@Service
@RequiredArgsConstructor
public class MessageRecordServiceImpl extends ServiceImpl<MessageRecordMapper, MessageRecord> implements MessageRecordService {

    private final ApplicationEventPublisher applicationEventPublisher;

    /**
     * 分页查询消息中心记录
     *
     * @param messageRecordPageDTO
     * @return
     */
    @Override
    public IPage<MessageRecordPageVO> pageMessageRecords(MessageRecordPageDTO messageRecordPageDTO) {
        return baseMapper.selectPage(messageRecordPageDTO, Wrappers.lambdaQuery(MessageRecord.class)
                .eq(Objects.nonNull(
                        messageRecordPageDTO.getReceiveUserId()),
                        MessageRecord::getReceiveUserId,
                        messageRecordPageDTO.getReceiveUserId())
                .eq(Objects.nonNull(
                        messageRecordPageDTO.getNoticeType()),
                        MessageRecord::getNoticeType,
                        messageRecordPageDTO.getNoticeType())
                .eq(Objects.nonNull(
                        messageRecordPageDTO.getMessageType()),
                        MessageRecord::getMessageType,
                        messageRecordPageDTO.getMessageType())
                .orderByDesc(MessageRecord::getCreatedTime));
    }

    /**
     * @param messageRecordInsertDTO 新增消息记录
     * @return
     */
    @Override
    public Long insert(MessageRecordInsertDTO messageRecordInsertDTO) {
        MessageRecord entity = new MessageRecord();
        BeanUtils.copyProperties(messageRecordInsertDTO, entity);
        entity.setCreatedTime(System.currentTimeMillis());
        Optional<MessageRecord> execute = EntityOperations.doCreate(baseMapper)
                .create(() -> entity)
                .update(e -> {
                })
                .successHook(thisEntity ->
                        applicationEventPublisher.publishEvent(new ISendMessageEvent.insertEvent(thisEntity)))
                .execute();
        return execute.map(MessageRecord::getId)
                .orElse(DbConstants.DEFAULT_LONG_VALUE);
    }

    @Override
    public Integer getMessageRecordNum() {
        return baseMapper.selectCount(new LambdaQueryWrapper<MessageRecord>()
                .eq(MessageRecord::getReceiveUserId, LoginUtils.getUserId())
                .eq(MessageRecord::getIsRead, 0));
    }

    @Override
    public R<Object> setRead(String taskIds, Integer noticeType, Integer messageType,String taskCommentId) {
        List<String> taskIdList = Arrays.asList(taskIds.split(","));
        List<MessageRecord> messageRecords = baseMapper.selectList(new LambdaQueryWrapper<MessageRecord>()
                .in(MessageRecord::getTaskId, taskIdList)
                .eq(StringUtils.isNotEmpty(taskCommentId),MessageRecord::getTaskCommentId, taskCommentId)
                .eq(MessageRecord::getNoticeType, noticeType)
                .eq(MessageRecord::getMessageType, messageType)
                .eq(MessageRecord::getReceiveUserId, LoginUtils.getUserId()));
        messageRecords.forEach(data -> {
            data.setIsRead(1);
            baseMapper.updateById(data);
        });
        return R.ok();
    }

}




